package main.Link;

import javax.swing.table.AbstractTableModel;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Locale;

public class LinkTable extends AbstractTableModel {
    private String[] columnNames = {"ID работы", "Наименование работы", "№ сметы", "№ строки", "Наименование сметной строки", "Кол-во", "Ед. изм.", "K", "K общ", "ФО"};
    private ArrayList<SQLLink> data = new ArrayList<>();
    Class[] types = new Class[]{
        String.class, String.class, String.class, String.class, String.class, Double.class, Double.class, Double.class, Boolean.class, int.class, int.class
    };

    boolean[] canEdit = new boolean [] { false, false, false, false, false, false, false, false, false, false };

    public LinkTable (ArrayList<SQLLink> data) {
        this.data = data;
    }

    public ArrayList<SQLLink> getSelectedLinks (int[] rows) {
        ArrayList<SQLLink> listLink = new ArrayList<>();
        for (int i : rows) {
            listLink.add(data.get(i));
        }
        return listLink;
    }

    public void setData (ArrayList<SQLLink> data) {
        this.data = data;
        fireTableDataChanged();
    }

    @Override
    public int getColumnCount() { return columnNames.length; }

    @Override
    public int getRowCount() { return data.size(); }

    @Override
    public String getColumnName(int col) { return columnNames[col]; }

    @Override
    public Object getValueAt(int row, int col) {
        if (data.get(row) != null) {
            SQLLink link = data.get(row);

            DecimalFormat df = new DecimalFormat();
            DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
            otherSymbols.setDecimalSeparator('.');
            otherSymbols.setGroupingSeparator(' ');

            df.setMaximumFractionDigits(6);
            df.setMinimumFractionDigits(0);
            df.setGroupingUsed(false);
            df.setDecimalFormatSymbols(otherSymbols);

            switch( col ) {
                case 0: return link.getTaskShortName();
                case 1: return link.getTaskName();
                case 2: return link.getSmNum();
                case 3: return link.getSmrNum();
                case 4: return link.getSmrName();
                case 5: return df.format(link.getSmrVol());
                case 6: return link.getUnit();
                case 7: return df.format(link.getFact());
                case 8: return df.format(link.getFactFull());
                case 9: return link.getPv();
                default: return "Unknown";
            }
        }
           return null;
    }

    public ArrayList <SQLLink> getData() { return data; }

    public int getTaskObjId(int row) {
        return data.get(row).getTaskId();
    }

    public int getSmRowId(int row) {
        return data.get(row).getSmrId();
    }

/*    public void fillFromSQL(ResultSet rs)
    {
    try {
      if ( rs == null ) return;

      while (rs.next())
      {
        this.data.add(new Object[] {"Сохранено", rs.getString(1),rs.getString(2), rs.getString(3), rs.getString(4), rs.getDouble(5), rs.getInt(6), rs.getInt(7)});
      }
    } catch (SQLException ex) {
      Logger.getLogger(LinkTable.class.getName()).log(Level.SEVERE, null, ex);
    }

    }

/*    public Object[] getLinkRow (ObjectId task, ObjectId smet)
    {
    for (Object[] row : data)
    {
      if ( task.toString().equals(row[6].toString()) && smet.toString().equals(row[7].toString()) )
        return row;
    }
    return null;
    }

    public void addRow (Object[] obj) { data.add(obj); }

    public void removeLinkRow (ObjectId task, ObjectId smet)
    {
    for (Object[] lrow : data)
    {
      if ( task.toString().equals(lrow[6].toString()) && smet.toString().equals(lrow[7].toString()) )
        data.remove(lrow);
    }
    }
    */

    @Override
    public Class getColumnClass(int columnIndex) { return types[columnIndex]; }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; }


    @Override
    public void setValueAt(Object value, int row, int col) {
        if (data.size() > row) {
            SQLLink link = data.get(row);

            switch( col )
            {
                case 0: link.setTaskShortName( (String) value ); break;
                case 1: link.setTaskName( (String) value ); break;
                case 2: link.setSmNum((String) value); break;
                case 3: link.setSmrNum( (String) value ); break;
                case 4: link.setSmrName( (String) value ); break;
                case 5: link.setSmrVol(new BigDecimal(value.toString())); break;
                case 6: link.setUnit((String) value); break;
                case 7: link.setFact(new BigDecimal(value.toString())); break;
                case 8: link.setFactFull(new BigDecimal(value.toString())); break;
                case 9: link.setPv((Boolean) value); break;
            }

        } else {

            SQLLink link = new SQLLink();

            switch( col )
            {
                case 0: link.setTaskShortName( (String) value ); break;
                case 1: link.setTaskName( (String) value ); break;
                case 2: link.setSmNum((String) value); break;
                case 3: link.setSmrNum( (String) value ); break;
                case 4: link.setSmrName( (String) value ); break;
                case 5: link.setSmrVol(new BigDecimal(value.toString())); break;
                case 6: link.setUnit((String) value); break;
                case 7: link.setFact(new BigDecimal(value.toString())); break;
                case 8: link.setFactFull(new BigDecimal(value.toString())); break;
                case 9: link.setPv((Boolean) value); break;

            }

            data.add(link);
        }
        //fireTableCellUpdated(row, col);

    }
}  

